﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Seesmic.Sdp.Utils;
using PTiRouZ.Net.SilverLight.Api.HelloTipi;
using System.Collections.ObjectModel;

namespace Seesmic.HelloTipi.Controls
{
    public partial class TimeLineItemTemplate : UserControl
    {
        private HelloTipiTimeLineItem _itemData;
        
        public TimeLineItemTemplate()
        {
            InitializeComponent();            
        }


        private void Username_Click(object sender, RoutedEventArgs e)
        {
            _itemData = (HelloTipiTimeLineItem)base.DataContext;

            apitxt api = new apitxt(_itemData.Account.Username, _itemData.Account.Password, "Seesmic Desktop 2");

            api.FamilyFriendsCompleted += new FamilyFriendsEventHandler(api_FamilyFriendsCompleted);

            api.FamilyFriends(_itemData.Site.name);                       
        }

        void api_FamilyFriendsCompleted(object sender, FamilyFriendsEventArgs e)
        {
            family_friends contact = (from family_friends c in (ObservableCollection<family_friends>)e.result
                     where c.id == _itemData.User.id
                     select c).FirstOrDefault();

            //HelloTipiPlugin.LogInfo("Show Profile : " + contact.name);


            HelloTipiUserProfile _profile = new HelloTipiUserProfile() { ItemData = _itemData, Contact = contact };

            HelloTipiPlugin.ShellService.ShowProfile(_profile);
        }

        private void hyperlink_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
            
            /*TwitterTimelineItem dataContext = (TwitterTimelineItem)base.DataContext;
            new TwitterAdapter().GetProfile((string)HyperlinkHelper.GetTag((DependencyObject)sender), false, delegate(TwitterUserProfile profile)
            {
                SynchronizationHelper.Post(delegate
                {
                    TwitterPlugin.ShellService.ShowProfile(profile);
                });
            });*/
        }

        private void TokenDefinition_ProvideUsernameInlines(object sender, ProvideInlinesEventArgs e)
        {
            string str = e.Match.Trim(new char[] { '@' });
            Run run = new Run();
            run.Text = "@";
            Hyperlink hyperlink = new Hyperlink();
            Run run2 = new Run();
            run2.Text = str;
            hyperlink.Inlines.Add(run2);
            hyperlink.Click += new RoutedEventHandler(this.hyperlink_Click);
            HyperlinkHelper.SetTag(hyperlink, str);
            e.Inlines = new Inline[] { run, hyperlink };
        }

        private void TokenDefinition_ProvideHashTagInlines(object sender, ProvideInlinesEventArgs e)
        {
            string str = e.Match.Trim(new char[] { '#' });
            Run run = new Run();
            run.Text = "#";
            Run run2 = new Run();
            run2.Text = str;
        }

    }
}
